package com.pcbly.web.persistence.system;

/**
 * @author carl.song
 * Copyright (c) 2010 Lightsoft, International.
 * All rights reserved.
 *
 */
import java.util.List;

import com.pcbly.web.components.utils.random.GUID;
import com.pcbly.web.bean.system.ModuleBean;
import com.pcbly.web.persistence.HibernateBaseDao;
import com.pcbly.web.components.hibernate.support.Page;

@SuppressWarnings("unchecked")
public class ModuleHibernateDao extends HibernateBaseDao<ModuleBean> implements
		ModuleDao {
	/**
	 * 
	 */
	private static final long serialVersionUID = -7293993575471189811L;

	protected Class<ModuleBean> getEntityClass() {
		return ModuleBean.class;
	}

	public ModuleHibernateDao() {
		super();
	}

	@Override
	public ModuleBean saveModuleBean(ModuleBean system) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug(system.getId());
		}
		String id = (new GUID()).toString();
		system.setId(id);
		getHibernateTemplate().save(system);
		return system;
	}

	@Override
	public void removeModuleBeanByIds(String ids) throws Exception {
		// TODO Auto-generated method stub
		ids = ids.replaceAll(",", "','");
		String hsql = "update sys_module set RECYCLED='1' where ID in( '" + ids
				+ "')";
		getSession().createSQLQuery(hsql).executeUpdate();

	}

	@Override
	public void removeModuleBeanById(String id) throws Exception {
		// TODO Auto-generated method stub
		id = id.replaceAll(",", "','");
		String hsql = "update sys_module set RECYCLED='1' where ID= '" + id
				+ "'";
		getSession().createSQLQuery(hsql).executeUpdate();

	}

	public Page<ModuleBean> findModuleBeanByCond(ModuleBean bean,
			Page<ModuleBean> page, String orderByName, boolean asc)
			throws Exception {
		StringBuilder sb = new StringBuilder();
		sb.append("select * from sys_module where RECYCLED='0' ");
		if (!(bean == null)) {
		}
		if (asc) {
			// criteria.addOrder(Order.asc(orderByName));
			sb.append(" order by  ");
			sb.append(orderByName);
			sb.append(" ");
		} else {
			// criteria.addOrder(Order.desc(orderByName));
			sb.append(" order by  ");
			sb.append(orderByName);
			sb.append(" desc ");
		}
		Object obj = getSession().createSQLQuery(
				"select count(ID) icount "
						+ removeSelect(removeOrders(sb.toString())) + " ")
				.uniqueResult();
		if (obj == null) {
			obj = "0";
		}

		page.setTotalCount(Integer.parseInt(String.valueOf(obj)));
		if (page.getTotalCount() > 0) {
			int first = page.getFirst();
			int lpage = first + page.getPageSize();
			if (lpage > page.getTotalCount()) {
				lpage = page.getTotalCount();
			}
			List<ModuleBean> beans = getSession().createSQLQuery(sb.toString())
					.addEntity(ModuleBean.class).list().subList(first, lpage);
			page.setResult(beans);
		}
		return page;
	}

	@Override
	public List<ModuleBean> findModuleBeansByCond(String menuId)
			throws Exception {
		// TODO Auto-generated method stub
		
		return null;
	}
}